namespace map2D
{
    /**时间控制器，用于控制时间速度，记录开始时间，总时长，时间进度百分比*/
    export class TimeTicker{
        private _startTime:number;
        public duration:number;
        // public curTime:number;
        private _scene:SceneManagerBase;
        private _running:boolean;
        public _paused:boolean;
        private _pauseStartTime:number;
        private _pauseTime:number;
        constructor(scene:SceneManagerBase)
        {
            let s = this;
            s._scene = scene;
            s._paused = false;
            s._pauseStartTime = s._pauseTime = 0;
            s.clear();
        }
        public set startTime(val:number)
        {
            this._running = val == val;
            this._startTime = val - this._pauseTime;
        }
        public get startTime():number
        {
            return this._startTime;
        }
        /**获取场景时间戳*/
        public getTimeTick():number
        {
            return this._scene.timeTick - this._pauseTime;
        }
        /**此计时器独立从startTime开始，经过的时间*/
        public getCurTime():number
        {
            let s = this;
            return s._scene.timeTick - s._pauseTime - s._startTime;
        }
        public getPercent():number
        {
            let s = this;
            if(!s._running)return 0;
            if(s.duration != s.duration || s.duration == 0)return 1;
            return s.getCurTime() / s.duration;
        }
        /**根据进度百分比重新设置计时器的起始时间*/
        public setPercent(val:number):void
        {
            let s= this;
            let t:number = s.duration * val;
            s.startTime = s.getTimeTick() - t;
        }
        public init(start:number,duration:number):TimeTicker
        {
            let s =this;
            s._startTime = start;
            s.duration = duration;
            s._running = start == start;
            return s;
            // s.curTime = timeTick;
        }
        public playRateUpdate():void
        {
            let s= this;
            if(s._scene._paused)
                return;
            s._startTime -= s._scene.timeDelta*(s._scene._playBackRate - 1);            
        }
        public clear():void
        {
            let s= this;
            s.duration = s._startTime = NaN;
            s._running = false;            
        }
        /**计时器是否运行中*/
        public running():boolean
        {
            return this._running;
        }

        public pause():void
		{
			let s =this;
			if(s._paused)return;
			s._paused = true;
			s._pauseStartTime = s._scene.timeTick;
		}
		public resume():void
		{
			let s = this;
			if(!s._paused)return;
			s._paused = false;
			s._pauseTime += s._scene.timeTick - s._pauseStartTime;
		}
        
    }
}